package com.yitiao.data_algorithms.leetcode.editor.cn;

import com.yitiao.data_algorithms.leetcode.editor.cn.base.ListNode;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.TreeNode;

class BinaryTreeTilt {
    // 2022-08-09 16:59:17
    //leetcode submit region begin(Prohibit modification and deletion)
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        int ans = 0;
        public int findTilt(TreeNode root) {
            dfs(root);
            return ans;
        }

        int dfs(TreeNode root){
            if (root==null)return 0;
            int l = dfs(root.left);
            int r = dfs(root.right);
            ans += Math.abs(r-l);
            return root.val + l+r;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}